package org.lulee007;

import org.jsonschema2pojo.*;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class Main {


    public static void main(String[] args) {

        if(args==null) {
            String currentDir = System.getProperty("user.dir") + "/java-gen";
//        String json="{\"deviceVersion\":\"8.1.12\",\"currentVersion\":\"1.0.6\",\"device\":\"2\",\"pageCurrent\":\"1\",\"pageOffset\":\"10\",\"orderColumn\":\"id\",\"orderType\":\"asc\"}";
//        String tempJsonPath = buildJsonPath(currentDir, json,"ParamObject");
            String tempJsonPath = "/Volumes/FilesHD/dev-tools/json2bean/java-gen/ParamObject.json";
            try {
                Jsonschema2Pojo.generate(new HmJsonGeneratorConfig("file:///" + tempJsonPath, currentDir));

            } catch (IOException e) {
                e.printStackTrace();
            }

        }else{
            if(args.length==0){
                System.out.println("路径不正确！");
            }else if(args.length==1)
            {
                String sourceFilePath=args[0];
                File sourceFile=new File(sourceFilePath);
                if(!sourceFile.exists() ||!sourceFile.isFile()){
                    System.out.println("源文件不存在："+sourceFilePath);
                    return;
                }
                String targetFolder=sourceFile.getParent();
                generator(sourceFilePath,targetFolder);
            }else {
                String sourceFilePath=args[0];
                String tartgetFolder=args[1];
                generator(sourceFilePath, tartgetFolder);
            }
        }
    }

    private static void generator(String sourceFilePath, String tartgetFolder) {
        File sourceFile=new File(sourceFilePath);
        File targetFile=new File(tartgetFolder);

        if(!sourceFile.exists() ||!sourceFile.isFile()){
            System.out.println("源文件不存在："+sourceFilePath);
            return;
        }
        if(!targetFile.exists() ||!targetFile.isDirectory()){
            System.out.println("保存目录不存在："+tartgetFolder);
            return;
        }
        try {
            Jsonschema2Pojo.generate(new HmJsonGeneratorConfig("file:///" + sourceFilePath, tartgetFolder));
            System.out.println("Java文件生成成功："+tartgetFolder+sourceFile.getName().replace(".json",".java"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String buildJsonPath(String currentDir, String json, String className) {
        File targetFolder = new File(currentDir);
        if (!targetFolder.exists()) targetFolder.mkdirs();
        String tempJsonPath = currentDir + "/" + className + ".json";
        FileWriter fw = null;
        try {
            fw = new FileWriter(tempJsonPath, false);
            fw.write(json, 0, json.length());
        } catch (IOException e) {
            tempJsonPath = null;
            e.printStackTrace();
        }
        try {
            if (fw != null)
                fw.flush();
        } catch (IOException e) {
            tempJsonPath = null;
            e.printStackTrace();
        }
        return tempJsonPath;
    }

}
